﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;


namespace CADShowTools
{
    public partial class CurrentShowForm :Form
    {

        [DllImport("user32")]
        public static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);
        [DllImport("user32")]
        public static extern uint GetWindowLong(IntPtr hwnd, int nIndex);
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        public static extern IntPtr SetActiveWindow(IntPtr handle);
        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();
        public const int GWL_EXSTYLE = -20;
        public const int WS_EX_TRANSPARENT = 32;
        public const int WS_EX_LAYERED = 524288;
        private readonly IntPtr _cadapphandle;

        public CurrentShowForm(IntPtr handle)
        {
            InitializeComponent();
            _cadapphandle = handle;
           
            //uint oldGWLEx = SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
            //uint intExTemp = GetWindowLong(dSkinLabel1.Handle, GWL_EXSTYLE);
            //SetWindowLong(dSkinLabel1.Handle, GWL_EXSTYLE, intExTemp ^ WS_EX_TRANSPARENT);
        }

        internal void Showinfo(string info, int showType)
        {
            HideAll();
            if (info == "") return;
            var pt = Cursor.Position;
            IntPtr foregetIntPtr = GetForegroundWindow();
            if (foregetIntPtr != this.Handle)
            {
                SetForegroundWindow(this.Handle);
            SetActiveWindow(_cadapphandle); 
            }
               
            pt.Offset(10, 10);

            switch (showType)
            {
                case 1: //lable
                    dSkinLabel1.Text = info;
                    dSkinLabel1.Visible = true;
                    dSkinLabel1.Location = pt;
                    break;
                case 2: //HTML标签
                    dSkinHtmlLabel1.Text = info;
                    dSkinHtmlLabel1.Visible = true;
                    dSkinHtmlLabel1.Location = pt;

                    break;
                case 3: //列表
                    dSkinListBox1.Items.Clear();
                    var fv = info.Split('\n');
                    foreach (var s in fv)
                    {
                        dSkinListBox1.Items.Add( s);
                    }
                    dSkinListBox1.Visible = true;
                    dSkinListBox1.Location = pt;
                    break;
            }
        }

        private void HideAll()
        {
            foreach (Control control in Controls)
            {
                control.Visible = false;
            }
        }

        private void dSkinLabel1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawLine(Pens.Red,e.ClipRectangle.Left, e.ClipRectangle.Top, e.ClipRectangle.Right,e.ClipRectangle.Top);
            e.Graphics.DrawLine(Pens.Red, e.ClipRectangle.Left, e.ClipRectangle.Bottom, e.ClipRectangle.Right, e.ClipRectangle.Bottom);
        }
    }
}